package com.graduation.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.graduation.common.domain.dto.PageDTO;
import com.graduation.common.exceptions.BizException;
import com.graduation.mapper.LawMapper;
import com.graduation.pojo.po.Category;
import com.graduation.pojo.po.Law;
import com.graduation.pojo.po.LegalDocument;
import com.graduation.pojo.query.LawQuery;
import com.graduation.pojo.vo.LawVO;
import com.graduation.service.CategoryService;
import com.graduation.service.LawService;
import com.graduation.service.LegalDocumentService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

@Slf4j
@Service
@RequiredArgsConstructor
public class LawServiceImpl extends ServiceImpl<LawMapper, Law> implements LawService {

    private final LegalDocumentService legalDocumentService;
    private final CategoryService categoryService;

    @Override
    public PageDTO<LawVO> pageLaws(LawQuery query) {
        // 使用自定义SQL进行联查
        Page<LawVO> page = new Page<>(query.getPageNum(), query.getPageSize());
        Page<LawVO> result = baseMapper.selectLawVOPage(page, query);
        return PageDTO.of(result.getPages(), result.getTotal(), result.getRecords());
    }

    @Override
    public Law getLawById(Long id) {
        Law law = getById(id);
        if (law == null) {
            throw new BizException("法律条款不存在");
        }
        return law;
    }

    @Override
    public void saveLaw(Law law) {
        // 检查法律代码是否已存在
        boolean exists = lambdaQuery()
                .eq(Law::getLawCode, law.getLawCode())
                .exists();
        if (exists) {
            throw new BizException("法律代码已存在");
        }

        // 验证关联的文档是否存在
        if (law.getDocumentId() != null) {
            LegalDocument document = legalDocumentService.getById(law.getDocumentId());
            if (document == null) {
                throw new BizException("关联的法律文档不存在");
            }
        }

        // 验证关联的分类是否存在
        if (law.getCategoryId() != null) {
            Category category = categoryService.getById(law.getCategoryId());
            if (category == null) {
                throw new BizException("关联的分类不存在");
            }
        }
        
        save(law);
    }

    @Override
    public void updateLaw(Law law) {
        Law existLaw = getById(law.getId());
        if (existLaw == null) {
            throw new BizException("法律条款不存在");
        }

        // 检查法律代码是否被其他条款使用
        if (StrUtil.isNotBlank(law.getLawCode()) && 
            !law.getLawCode().equals(existLaw.getLawCode())) {
            boolean exists = lambdaQuery()
                    .eq(Law::getLawCode, law.getLawCode())
                    .ne(Law::getId, law.getId())
                    .exists();
            if (exists) {
                throw new BizException("法律代码已存在");
            }
        }

        // 验证关联的文档是否存在
        if (law.getDocumentId() != null && !law.getDocumentId().equals(existLaw.getDocumentId())) {
            LegalDocument document = legalDocumentService.getById(law.getDocumentId());
            if (document == null) {
                throw new BizException("关联的法律文档不存在");
            }
        }

        // 验证关联的分类是否存在
        if (law.getCategoryId() != null && !law.getCategoryId().equals(existLaw.getCategoryId())) {
            Category category = categoryService.getById(law.getCategoryId());
            if (category == null) {
                throw new BizException("关联的分类不存在");
            }
        }
        
        updateById(law);
    }

    @Override
    public void deleteLaw(Long id) {
        Law law = getById(id);
        if (law == null) {
            throw new BizException("法律条款不存在");
        }
        removeById(id);
    }

}
